package com.ruoyi.business.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.business.model.PickerModel;
import com.ruoyi.business.model.TreeModel;
import com.ruoyi.business.request.ApiTaskReq;
import com.ruoyi.business.response.ApiPlanLayerRes;
import com.ruoyi.business.response.ApiResearchPlanRes;
import com.ruoyi.business.response.ApiTaskPointRes;
import com.ruoyi.business.response.ApiTaskRes;
import com.ruoyi.business.service.IResearchPlanService;
import com.ruoyi.business.service.ITaskService;
import com.ruoyi.common.core.domain.PageResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author liutao
 * @version 1.0
 * @description: TODO
 * @date 2024/3/20 16:34
 */
@RestController
@RequestMapping("/api/research/plan")
public class ResearchPlanApi {
    @Resource
    private IResearchPlanService researchPlanService;
    @Resource
    private ITaskService taskService;


    @GetMapping("/page")
    public R<PageResult<ApiResearchPlanRes>> pageResearchPlan(ApiTaskReq req) {
        return R.ok(researchPlanService.pageResearchPlan(req));
    }

    /**
     * 有效期内计划
     * @return
     */
    @GetMapping("/select")
    public R<List<List<Map<String, Object>>>> selectResearchPlan() {
        List<Map<String, Object>> planList = researchPlanService.selectValidityPeriodByUserId(SecurityUtils.getUserId());
        if (CollUtil.isNotEmpty(planList)) {
            for (Map<String, Object> map : planList) {
                Long id = MapUtil.getLong(map, "id");
                List<ApiTaskPointRes> taskPointResList = taskService.getTaskPointByPlanId(id);
                map.put("taskPointList", taskPointResList);
            }
        }
        List<Map<String, Object>> planNotTaskList = researchPlanService.selectValidityPeriodNotTaskByUserId(SecurityUtils.getUserId());

        List<List<Map<String, Object>>> list = Lists.newArrayList();
        list.add(planList);
        list.add(planList);
        return R.ok(list);
    }

    @GetMapping("/select/all")
    public R<List<Map<String, Object>>> selectAllResearchPlan() {
        List<Map<String, Object>> planList = researchPlanService.selectByUserId(SecurityUtils.getUserId());
        List<Long> repeatPlanList = Lists.newArrayList();
        List<Map<String, Object>> newList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(planList)) {
            for (Map<String, Object> map : planList) {
                Long id = MapUtil.getLong(map, "id");
                repeatPlanList.add(id);
                String name = MapUtil.getStr(map, "plan_name");
                boolean expire = MapUtil.getBool(map, "expire");
                Map<String, Object> map1 = Maps.newHashMap();
                map1.put("value", id);
                map1.put("name", name);
                map1.put("expire", expire);
                if (expire) {
                    map1.put("color", "#ffaa7f");
                }
                newList.add(map1);
            }
        }
        List<Map<String, Object>> planNotTaskList = researchPlanService.selectNotTaskPlanByUserId(SecurityUtils.getUserId());
        if (CollUtil.isNotEmpty(planNotTaskList)) {
            for (Map<String, Object> map : planNotTaskList) {
                Long id = MapUtil.getLong(map, "id");
                if (CollUtil.contains(repeatPlanList, id)) {
                    continue;
                }
                String name = MapUtil.getStr(map, "plan_name");
                boolean expire = MapUtil.getBool(map, "expire");
                Map<String, Object> map1 = Maps.newHashMap();
                map1.put("value", id);
                map1.put("name", name);
                map1.put("expire", expire);
                if (expire) {
                    map1.put("color", "#ffaa7f");
                }
                newList.add(map1);
            }
        }

//        List<List<Map<String, Object>>> list = Lists.newArrayList();
//        list.add(newList);
        return R.ok(newList);
    }

    @GetMapping("/select/valid/period")
    public R<List<Map<String, Object>>> selectValidityPeriodByUserId() {
        List<Map<String, Object>> planList = researchPlanService.selectValidityPeriodByUserId(SecurityUtils.getUserId());
        List<Map<String, Object>> newList = Lists.newArrayList();
        List<Long> repeatPlanList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(planList)) {
            for (Map<String, Object> map : planList) {
                Long id = MapUtil.getLong(map, "id");
                repeatPlanList.add(id);
                String name = MapUtil.getStr(map, "plan_name");
                Map<String, Object> map1 = Maps.newHashMap();
                map1.put("value", id);
                map1.put("name", name);
                newList.add(map1);
            }
        }
        List<Map<String, Object>> planNotTaskList = researchPlanService.selectValidityPeriodNotTaskByUserId(SecurityUtils.getUserId());
        if (CollUtil.isNotEmpty(planNotTaskList)) {
            for (Map<String, Object> map : planNotTaskList) {
                Long id = MapUtil.getLong(map, "id");
                if (CollUtil.contains(repeatPlanList, id)) {
                    continue;
                }
                String name = MapUtil.getStr(map, "plan_name");
                Map<String, Object> map1 = Maps.newHashMap();
                map1.put("value", id);
                map1.put("name", name);
                newList.add(map1);
            }
        }
        return R.ok(newList);
    }

    @GetMapping("/layer")
    public R<List<ApiPlanLayerRes>> getPlanLayer(Long researchPlanId) {
        if (researchPlanId == null) {
            return R.ok(Lists.newArrayList());
        }
        return R.ok(researchPlanService.getPlanLayer(researchPlanId));
    }

    @GetMapping("/picker/layer")
    public R<List<PickerModel>> getPlanPickerLayer(Long researchPlanId) {
        if (researchPlanId == null) {
            return R.ok(Lists.newArrayList());
        }
        return R.ok(researchPlanService.getPlanPickerLayer(researchPlanId));
    }
}
